/* =========================================
			
		FCNPC - Fully Controllable NPC
			----------------------

	- File: Utils.cpp
	- Author(s): OrMisicL

  =========================================*/

#include "Main.h"

void CUtils::GetPluginError(BYTE byteError, char *szError)
{
	switch(byteError)
	{
		case 1:
			strcpy(szError, "File \"scriptfiles/FCNPC/ZMap.hmap\" is not found");
			break;

		case 2:
			strcpy(szError, "Failed to create PlayerManager instance");
			break;

		case 3:
			strcpy(szError, "Failed to create NodeManager instance");
			break;

		case 4:
			strcpy(szError, "Failed to create damage thread");
			break;

		case 5:
			strcpy(szError, "Failed to create RPCParams instance");
			break;

		case 6:
			strcpy(szError, "Unknown samp server version. FCNPC only supports 0.3z versions");
			break;

		default:
			strcpy(szError, "");
			break;
	}
}

void CUtils::UnProtect(DWORD dwAddress, size_t sSize)
{
	// Unprotect the address
#ifdef _WIN32
	DWORD dwOldProtection;
	VirtualProtect((LPVOID)dwAddress, sSize, PAGE_EXECUTE_READWRITE, &dwOldProtection);
#else
	mprotect((void*)(((int)dwAddress / 4096) * 4096), 4096, PROT_WRITE | PROT_READ | PROT_EXEC);
#endif
}

void CUtils::FCNPCSleep(DWORD dwMs)
{
	// Sleep
#ifdef _WIN32
	Sleep(dwMs);
#else
	usleep((dwMs) * 1000);
#endif
}

// Linux GetTickCount
#ifndef _WIN32

#include "sys/time.h"
timeval start_time;
timeval now_time;

void TickCountLoad()
{
	// Get the starting time
	gettimeofday(&start_time, 0);
}

int GetTickCount()
{
	// Get the time elapsed since the start
	gettimeofday(&now_time,0);
	return (now_time.tv_usec - start_time.tv_usec) / 1000 + 1000 * (now_time.tv_sec - start_time.tv_sec);
}

#endif
